package com.imooc.restful.controller;

import com.imooc.restful.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2021/2/25 21:51
 * Description: restful
 */
@RestController
@RequestMapping("/restful")
//@CrossOrigin(origins = {"http://localhost:8080","http://www.imooc.com"}) //设置跨域访问的范围 说明哪些其他域范围送过来的请求是允许被访问这个RestfulController的
//@CrossOrigin(origins = "*" maxAge=3600)//maxAge:设置 预检请求的处理结果 的缓存时间为3600秒
public class RestfulController {

    @GetMapping("/request")
    //@ResponseBody
    public String doGetRequest() {
        return "{\"message\":\"返回查询结果\"}";
    }

    @PostMapping("/request/{rid}")
    //@ResponseBody
    public String doPostRequest(@PathVariable("rid") Integer requestId) {
        return "{\"message\":\"数据新建成功\",\"requestId\":" + requestId + "}";
    }

    @PutMapping("/request")
    //@ResponseBody
    public String doPutRequest() {
        return "{\"message\":\"数据修改成功\"}";
    }

    @DeleteMapping("/request")
    //@ResponseBody
    public String doDeleteRequest() {
        return "{\"message\":\"数据删除成功\"}";
    }

    @GetMapping("/person")
    public Person findPersonById(Integer id) {
        Person person = new Person();
        if (id == 1) {
            person.setName("lily");
            person.setAge(23);
            person.setBirthday(new Date());
        } else if (id == 2) {
            person.setName("smith");
            person.setAge(30);
            person.setBirthday(new Date());
        }
        return person;

    }

    @GetMapping("/persons")
    public List<Person> findPersons() {
        List list = new ArrayList();

        Person p1 = new Person();
        p1.setName("lily");
        p1.setAge(23);
        p1.setBirthday(new Date());

        Person p2 = new Person();
        p2.setName("smith");
        p2.setAge(30);
        p2.setBirthday(new Date());

        list.add(p1);
        list.add(p2);

        return list;

    }
}
